package com.mcxx.modules.extremepoverty.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.family.dto.SalvationObjectDTO;
import com.mcxx.modules.miniSalvation.family.param.SalvationObjectParam;
import com.mcxx.modules.miniSalvation.order.dto.ExInsuranceDrawUpDTO;
import com.mcxx.modules.miniSalvation.order.param.InsuranceParam;
import com.mcxx.modules.miniSalvation.order.service.read.BusiInsureDetailReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.modules.miniSalvation.order.service.write.BusiInsureDetailWriteService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.ZipInfoUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.mcxx.util.Constant;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Validated
@Api("护理保险管理")
@RestService("extremePoverty/insurance/v2")
public class ExNurseInsuranceController extends DefaultRestService {


    @Resource
    private BusiInsureDetailWriteService exInsuranceDrawUpWriteService;

    @Resource
    private BusiInsureDetailReadService insuranceDrawUpReadService;

    @Autowired
    private FileService fileService;

    @Autowired
    private OrderReadService orderReadService;




    @ApiOperation("新增保险")
    @PostMapping("/addInsurance")
    public void addInsurance(ExInsuranceDrawUpDTO insurance){
        log.info("新增保险，参数是：{}",insurance);
        int rstFlag = exInsuranceDrawUpWriteService.addInsurance(insurance,getCurUserAccount());
        log.info("新增保险，结果是：{}",rstFlag);
    }


    @ApiOperation("新增保险明细")
    @PostMapping("/addInsuranceDetails")
    public void addInsuranceDetails(String insuranceId,String flag, String objIds,String areaCode){
        log.info("新增保险明细，保险id参数是：{}，特困对象id集是：{}",insuranceId,objIds);
        int restFlag = exInsuranceDrawUpWriteService.addInsuranceDetails(insuranceId,flag,objIds,areaCode);
        setData(restFlag);
        log.info("新增保险明细，结果是：{}",restFlag);

    }


    @ApiOperation("更新保险明细")
    @PostMapping("/updateInsuraceDetails")
    public void updateInsuraceDetails(String insuranceId,String objIds){
        log.info("更新保险明细，保险id是：{}，对象id组是：{}",insuranceId,objIds);
       int restFlag =  exInsuranceDrawUpWriteService.updateInsuraceDetails(insuranceId,objIds);
       log.info("更新保险明细，结果是：{}",restFlag);
    }



    @ApiOperation("保存已选对象")
    @PostMapping("/saveSelectObj")
    public void saveSelectObj(){

    }


    @ApiOperation("确认保险")
    @PostMapping("/confirm")
    public void confirmInsurance(String insuranceId,String userId){
        log.info("确认保险，参数是：{}，userid：{}",insuranceId,userId);
       int restFlag =  exInsuranceDrawUpWriteService.confirm(insuranceId,userId);
       setData(restFlag);
       log.info("确认成功，结果是：{}",restFlag);
    }

    @ApiOperation("删除保险")
    @PostMapping("/del")
    public void delInsurance(String insuranceId){
        log.info("删除，保险参数是：{}",insuranceId);
        int restFlag = exInsuranceDrawUpWriteService.del(insuranceId);
        log.info("删除，结果是：{}",restFlag);
    }



    @ApiOperation("编辑保险")
    @PostMapping("/update")
    public void updateInsurance(ExInsuranceDrawUpDTO exInsuranceDrawUpDTO){
        log.info("编辑保险，保险id参数是：{}",exInsuranceDrawUpDTO);
        int restFlag = exInsuranceDrawUpWriteService.updateInsurance(exInsuranceDrawUpDTO);
        log.info("编辑保险完成，结果是：{}",restFlag);
    }

    @ApiOperation("移除所选对象")
    @PostMapping("/delSelectObj")
    public void delSelectObj(String ids){
        log.info("移除所选对象，对象id是：{}",ids);
        int restFlag = exInsuranceDrawUpWriteService.delSelectObj(ids);
        setData(restFlag);
        log.info("移除完成，结果是：{}",restFlag);
    }




//        log.info("查询供养对象列表，参数是：{},{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));

//        param.setBusinessType(Constant.BusinessCode.TEKUN);

//        log.info("查询供养对象列表结束，参数是：{}", JSON.toJSONString(restFlag));








    @ApiOperation("保险机构查询")
    @PostMapping("/getInsuranceOrg")
    public void getInsuranceOrg(InsuranceParam param, PageParam pageParam){
        log.info("查询保险机构查询,查询参数：{},分页参数：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page  = insuranceDrawUpReadService.getInsuranceOrg(param,pageParam,getCurUserAccount());
        setPageBean(page);
        log.info("查询保险机构完成，结果是：{}",page);

    }

    @ApiOperation("获取保险机构的状态")
    @PostMapping("/getInsuranceType")
    public void getInsuranceType(String insuranceId){
        log.info("获取保险机构的状态查询，保险id参数是：{}",insuranceId);
        String insuranceType = insuranceDrawUpReadService.getInsuranceType(insuranceId);
        setData(insuranceType);
        log.info("获取保险机构状态完成，结果是：{}",insuranceType);
    }

    @ApiOperation("获取保险机构信息")
    @PostMapping("/getInsuranceInfo")
    public void getInsuranceInfo(String insuranceId){
        log.info("获取保险机构信息查询，保险id参数是：{}",insuranceId);
        ExInsuranceDrawUpDTO insuranceInfo = insuranceDrawUpReadService.getInsuranceInfo(insuranceId);
        if(null != insuranceInfo){
            String imgUrl = fileService.getFileUrl(insuranceInfo.getImgUrl());
            insuranceInfo.setImgUrl(imgUrl);
        }
        setData(insuranceInfo);
        log.info("获取保险机构信息完成，结果是：{}",insuranceInfo);
    }



    @PostMapping({"/downLoadInsuranceList"})
    public void exportInsurance(HttpServletResponse response,@RequestBody InsuranceParam param) throws IOException {
        ExcelInfo excelInfo = insuranceDrawUpReadService.exportInsuranceExcel(param.getExportType(),param,getCurUserAccount());
        if(null != excelInfo){
            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }


    @PostMapping("/exportIncludeObj")
    @ApiOperation("导出已入保险对象")
    public void exportIncludeObj(HttpServletResponse response,@RequestBody InsuranceParam param) throws IOException {
        ExcelInfo excelInfo = insuranceDrawUpReadService.exportDetailsExcel(param.getExportType(),param.getIdsList(),param,getCurUserAccount());
        if(null != excelInfo){
            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }


    @PostMapping("getSupplyObjectListNotAdded")
    @ApiOperation("查询供养对象列表")
    public void getSupplyObjectList2(SalvationObjectParam param, PageParam pageParam) {
        log.info("查询供养对象列表，参数是：{},{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page<SalvationObjectDTO> page = exInsuranceDrawUpWriteService.getSupplyObjectListNotAdded(param, pageParam, accountVo);
        setPageBean(page);
        log.info("查询供养对象列表结束，参数是：{}", JSON.toJSONString(page));
    }



    @PostMapping("/getInsuranceObj")
    @ApiOperation("查询已纳入保险的对象")
    public void getInsuranceObj(SalvationObjectParam param, PageParam pageParam){
        log.info("查询供养对象列表，参数是：{},{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page<SalvationObjectDTO> page = orderReadService.getInsuranceObj(param, pageParam, accountVo);
        setPageBean(page);
        log.info("查询供养对象列表结束，参数是：{}", JSON.toJSONString(page));
    }

}
